package com.dap.system.office.service.impl;

import java.util.List;
import java.util.UUID;

import com.dap.common.core.exception.CustomException;
import com.dap.common.core.utils.DateUtils;
import com.dap.common.core.utils.SecurityUtils;
import com.dap.common.core.utils.StringUtils;
import com.dap.system.api.domain.SysUser;
import com.dap.system.office.domain.*;
import com.dap.system.office.domain.vo.TClassSelectorVo;
import com.dap.system.office.mapper.TSemesterMapper;
import com.dap.system.office.mapper.TStudentMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.dap.system.office.mapper.TClassMapper;
import com.dap.system.office.service.ITClassService;

import javax.annotation.Resource;

/**
 * 班级Service业务层处理
 *
 * @author dap
 * @date 2021-11-11
 */
@Service
public class TClassServiceImpl implements ITClassService {
    @Resource
    private TClassMapper tClassMapper;
    @Resource
    private TSemesterMapper tSemesterMapper;
    @Resource
    private TStudentMapper tStudentMapper;


    /**
     * 查询班级
     *
     * @param id 班级ID
     * @return 班级
     */
    @Override
    public TClass selectTClassById(Long id) {
        return tClassMapper.selectTClassById(id);
    }

    /**
     * 查询班级列表
     *
     * @param tClass 班级
     * @return 班级
     */
    @Override
    public List<TClass> selectTClassList(TClass tClass) {
        return tClassMapper.selectTClassList(tClass);
    }

    /**
     * 分页查询班级列表
     *
     * @param tClass 班级
     * @return 班级
     */
    @Override
    public List<TClass> selectTClassListPage(TClass tClass) {
        PageHelper.startPage(tClass.getPageNum(), tClass.getPageSize());
        List<TClass> list = tClassMapper.selectTClassList(tClass);
        PageInfo<TClass> tPageInfo = new PageInfo<>(list);
        List<TClass> dataList = tPageInfo.getList();
        return dataList;
    }

    /**
     * 新增班级
     *
     * @param tClass 班级
     * @return 结果
     */
    @Override
    public int insertTClass(TClass tClass) {
        List<TClass> tClasses = tClassMapper.selectTClassList(tClass);
        if (tClasses == null || tClasses.size() == 0) {
            tClass.setGraId(tClass.getGraId());
            tClass.setGradeName(tClass.getGradeName());
            tClass.setSemId(tClass.getSemId());
            tClass.setSemName(tClass.getSemName());
            tClass.setCreateTime(DateUtils.getNowDate());
            tClass.setDelFlag("0");
            tClass.setCreateBy(SecurityUtils.getUsername());
//            TSemester semester = tSemesterMapper.selectTSemesterByName(tClass.getSemName());
//            Long id = semester.getId();

            return tClassMapper.insertTClass(tClass);
        }
        return 0;
    }

    /**
     * 修改班级
     *
     * @param tClass 班级
     * @return 结果
     */
    @Override
    public int updateTClass(TClass tClass) {
        TClass tClass1 = new TClass();
        tClass1.setClassName(tClass.getClassName());
        tClass1.setGraId(tClass.getGraId());
        tClass1.setSemId(tClass.getSemId());
        List<TClass> tClasses = (List<TClass>) tClassMapper.selectTClassList(tClass1);
        if (tClasses.size() > 0) {
            throw new CustomException("该班级已存在,修改失败");
        }
        tClass.setUpdateBy(SecurityUtils.getUsername());
        tClass.setUpdateTime(DateUtils.getNowDate());
        return tClassMapper.updateTClass(tClass);
    }

    /**
     * 批量删除班级
     *
     * @param ids 需要删除的班级ID
     * @return 结果
     */
    @Override
    public int deleteTClassByIds(Long[] ids) {
        for (Long id : ids) {
            TStudent student = new TStudent();
            student.setId(id);
            List<TStudent> tStudents = tStudentMapper.selectTStudentList(student);
            if (tStudents.size() > 0) {
                throw new CustomException("该班级下存在学生，禁止删除");
            }
        }
        return tClassMapper.deleteTClassByIds(ids);
    }

    /**
     * 删除班级信息
     *
     * @param id 班级ID
     * @return 结果
     */
    @Override
    public int deleteTClassById(Long id) {
        return tClassMapper.deleteTClassById(id);
    }

    @Override
    public List<TClassSelectorVo> selectTClassNameList(TClass tClass) {
        return tClassMapper.selectTClassNameList(tClass);
    }

    @Override
    public List<TClassSelectorVo> selectTGradeNameList(TClass tClass) {
        return tClassMapper.selectTGradeNameList(tClass);

    }

    @Override
    public String importClass(List<TClass> classList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(classList) || classList.size() == 0) {
            throw new CustomException("导入数据不能为空！");
        } else {
            for (TClass classs : classList) {
                TClass tclass = tClassMapper.TClassById(classs.getId());
                if (tclass != null) {
                    Inpot byInfo = tSemesterMapper.findByInfoClass(tclass.getGradeName(), tclass.getSemName());
                    TClass newClass = new TClass();
                    newClass.setId(tclass.getId());
                    newClass.setGraId(byInfo.getGradeId());
                    newClass.setSemId(byInfo.getSemesterId());
                    newClass.setUpdateBy(SecurityUtils.getUsername());
                    newClass.setUpdateTime(DateUtils.getNowDate());
                    newClass.setDelFlag("0");
                    tClassMapper.updateTClass(classs);
                } else {
                    Inpot byInfo = tSemesterMapper.findByInfoClass(classs.getGradeName(), classs.getSemName());
                    if (byInfo == null) {
                        throw new CustomException("所选择的学期或者年级不存在！");
                    }
                    TClass newClass = new TClass();
                    newClass.setClassName(classs.getClassName());
                    newClass.setGraId(byInfo.getGradeId());
                    newClass.setSemId(byInfo.getSemesterId());
                    newClass.setCreateBy(SecurityUtils.getUsername());
                    newClass.setCreateTime(DateUtils.getNowDate());
                    newClass.setDelFlag("0");
                    tClassMapper.insertTClass(newClass);
                }
            }
            return "导入成功";
        }
    }
}
